package com.yic.module.base.controller.admin.factory;

import com.yic.framework.common.enums.CommonStatusEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.base.controller.admin.factory.vo.*;
import com.yic.module.base.dal.dataobject.factory.FactoryDO;
import com.yic.module.base.convert.factory.FactoryConvert;
import com.yic.module.base.service.factory.FactoryService;

@Tag(name = "管理后台 - 工厂")
@RestController
@RequestMapping("/base/factory")
@Validated
public class FactoryController {

    @Resource
    private FactoryService factoryService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂")
    @PreAuthorize("@ss.hasPermission('base:factory:create')")
    public CommonResult<Long> createFactory(@Valid @RequestBody FactoryCreateReqVO createReqVO) {
        return success(factoryService.createFactory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂")
    @PreAuthorize("@ss.hasPermission('base:factory:update')")
    public CommonResult<Boolean> updateFactory(@Valid @RequestBody FactoryUpdateReqVO updateReqVO) {
        factoryService.updateFactory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('base:factory:delete')")
    public CommonResult<Boolean> deleteFactory(@RequestParam("id") Long id) {
        factoryService.deleteFactory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('base:factory:query')")
    public CommonResult<FactoryRespVO> getFactory(@RequestParam("id") Long id) {
        FactoryDO factory = factoryService.getFactory(id);
        return success(FactoryConvert.INSTANCE.convert(factory));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工厂列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('base:factory:query')")
    public CommonResult<List<FactoryRespVO>> getFactoryList(@RequestParam("ids") Collection<Long> ids) {
        List<FactoryDO> list = factoryService.getFactoryList(ids);
        return success(FactoryConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取工厂精简信息列表", description = "只包含被开启的工厂，主要用于前端的下拉选项")
    public CommonResult<List<FactorySimpleRespVO>> getSimpleFactoryList() {
        // 获得工厂列表，只要开启状态的
        List<FactoryDO> list = factoryService.getFactoryList(null, Collections.singleton(CommonStatusEnum.ENABLE.getStatus()));
        // 排序后，返回给前端
        list.sort(Comparator.comparing(FactoryDO::getSort));
        return success(FactoryConvert.INSTANCE.convertList03(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂分页")
    @PreAuthorize("@ss.hasPermission('base:factory:query')")
    public CommonResult<PageResult<FactoryRespVO>> getFactoryPage(@Valid FactoryPageReqVO pageVO) {
        PageResult<FactoryDO> pageResult = factoryService.getFactoryPage(pageVO);
        return success(FactoryConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂 Excel")
    @PreAuthorize("@ss.hasPermission('base:factory:export')")
    @OperateLog(type = EXPORT)
    public void exportFactoryExcel(@Valid FactoryExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<FactoryDO> list = factoryService.getFactoryList(exportReqVO);
        // 导出 Excel
        List<FactoryExcelVO> datas = FactoryConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "工厂.xls", "数据", FactoryExcelVO.class, datas);
    }

}
